﻿Public Class frmUserStoriesSearch

    Dim strNameForm As String = "Search User Story"
    Public strSqlWhere As String = " 1=1 "

    Public Function Initialize() As Boolean
        Try
            Me.Text = strNameForm

            cboSprints.Load("Select * From Sprints", "Name", gConnectionStringAgenda.ToString)
            cboProjects.Load("Select * From Projects", "Name", gConnectionStringAgenda.ToString)
            Dim listStatus As New List(Of Base.GlobalConst)
            listStatus.Add(US_STATUS_PLANNED)
            listStatus.Add(US_STATUS_ONGOING)
            listStatus.Add(US_STATUS_ONHOLD)
            listStatus.Add(US_STATUS_DONE)
            listStatus.Add(US_STATUS_CANCELLED)
            listStatus.Add(US_STATUS_NEXT)
            cboStatus.DataSource = listStatus
            cboStatus.ValueMember = "NUMBER"
            cboStatus.DisplayMember = "NAME"
            listStatus = Nothing

            AddHandler cboSprints.OnInsert, AddressOf InterfaceManager.Sprints_OnInsert
            AddHandler cboSprints.OnModify, AddressOf InterfaceManager.Sprints_OnModify
            AddHandler cboProjects.OnInsert, AddressOf InterfaceManager.Projects_OnInsert
            AddHandler cboProjects.OnModify, AddressOf InterfaceManager.Projects_OnModify

            Return True
        Catch ex As Exception
            Base.ShowError.Show("Initialize", Me.Name, "Initialize", ex)
            Return False
        End Try
    End Function

#Region "Commands"

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
        Try
            If txtName.Text <> "" Then strSqlWhere &= String.Format(" and Name like '{0}%'", txtName.Text)
            If txtNumber.Text <> "" Then strSqlWhere &= String.Format(" and Number like '{0}%'", txtNumber.Text)
            If txtDescription.Text <> "" Then strSqlWhere &= String.Format(" and Description like '{0}%'", txtDescription.Text)
            If txtDone.Text <> "" Then strSqlWhere &= String.Format(" and Done like '{0}%'", txtDone.Text)
            If txtPriority.Text <> "" Then strSqlWhere &= String.Format(" and Priority = {0}", txtPriority.GetIntNumber)
            If cboStatus.SelectedIndex <> -1 Then strSqlWhere &= String.Format(" and Status={0}", cboStatus.SelectedValue)
            If cboProjects.SelectedIndex <> -1 Then strSqlWhere &= String.Format(" and ProjectsID={0}", cboProjects.SelectedValue)
            If cboSprints.SelectedIndex <> -1 Then strSqlWhere &= String.Format(" and SprintsID={0}", cboSprints.SelectedValue)

            Me.DialogResult = DialogResult.OK
        Catch ex As Exception
            Base.ShowError.Show("Search", Me.Name, "cmdSearch_Click", ex)
        End Try
    End Sub

#End Region

#Region "Form events"

    Private Sub Me_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.DialogResult = DialogResult.Cancel
        End If
    End Sub

#End Region

End Class